package KK.EMVC.View
{
	import KK.GameBase.BaseUI;
	import KK.GameBase.IEventBase;
	import KK.GameBase.IMessageFollowers;
	import KK.GameBase.IMessageQueue;
	import KK.GameBase.MSGEvent;
	import KK.System.ArgumenError;
	import KK.System.IApplicationManager;
	import KK.System.System;
	import KK.Time.FrameWheel;
	import KK.Time.IWheel;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	
	
	public class View extends BaseUI implements IView , IWheel
	{ 
		static public const NO_RESOURCES:int=-1;//没有资源 
		static public const SUCCESS:int=1;//成功
		
		static public const RESOURCE:int=1;//是否设置资源
		static public const EVENT:int=2;//是否绑定事件
		
		private var _frameState:uint;//帧状态 
		private var app:ApplicationDomain;//applicationdomain
		private var resManager:IApplicationManager;//资源
		protected var loaderQueue:Dictionary;
		
		private var state:int;//状态
		public function View(frameRate:uint=24)
		{
			//TODO: implement function
			super();
			
			_frameRate=frameRate;
			startFrameWheel();
			init();
		}
		private function init():void{
			SetMC(new Sprite());
			loaderQueue=new Dictionary(); 
		}
		
		
		override public function BindEvent(_event:IMessageQueue):Boolean{
			
			
			var result:Boolean=super.BindEvent(_event);
			initUI();
			return result;
		}
		
		private function initUI():void{
			state|=EVENT;
		}
		
		
		protected function AddSwfLoader(url:String,Complete:Function):void{
			
			loaderQueue[url]=Complete;
			var event:Event=this.resManager.GetLoaded(url);
			if(event)
			{
				RemoveMessage(event.type);
				System.AddBytesLoader(event.target.bytes,BytesLoaded,url); 
			}else{
				
				AttentionMessage(url,Loaded);
			}
			
			//			System.AddSwfLoader(url,BytesLoaded); 
		}
		private function BytesLoaded(event:Event):void{
			this.resManager.AddLoadedList(event.target.loader.name,event);
			if(loaderQueue[event.target.loader.name]){
				loaderQueue[event.target.loader.name](event);
			} 
			delete loaderQueue[event.target.loader.name];
		}
		
		private function Loaded(event:MSGEvent):void{
			RemoveMessage(event.type);
			
			AddSwfLoader(event.type,loaderQueue[event.type]);
			//			System.AddBytesLoader(event.target.bytes,loaderQueue[event.type],event.type);                                             
		}
		
		
		
		/*
		public function Update(event:MSGEvent):void
		{
		//TODO: implement function
		if(msgList&&msgList[event.type]is Function)
		{
		msgList[event.type](event);
		}
		}
		
		
		override protected function Publish(event:MSGEvent):void{
		if(this._event){
		this._event.Publish(event);
		}else{
		ArgumenError.checkArgumen("Publish Event Faild!");
		}
		}
		public function RemoveMessage(type:String):void{
		if(this._event){
		delete msgList[type];
		this._event.RemoveMessage(type,this);
		}else{
		ArgumenError.checkArgumen("RemoveMessage Message Faild!");
		}
		}
		public function AttentionMessage(type:String,callback:Function=null):void
		{
		if(this._event){
		msgList[type]=callback;
		this._event.AttentionMessage(type,this);
		}else{
		ArgumenError.checkArgumen("AttentionMessage Message Faild!");
		}
		}
		*/
		public function Show():void{
			this.mc.visible=true;
		}
		public function Hide():void{ 
			this.mc.visible=false;
		}
		public function ReSize(width:int,height:int):void{
			
		}
		
		/**
		 * 设置加载资源  
		 * @param vie
		 * @return 
		 * 
		 */
		public function SetResource(view:IView):int{
			var result:int=NO_RESOURCES;
			if(this.resManager){
				view.setAppManager(this.resManager);
				result=SUCCESS;
			}
			return result;
		}
		
		/**
		 * 设置applicationdomain 
		 * @param app
		 * @return 
		 * 
		 */
		public function SetApp(app:ApplicationDomain):void
		{
			this.app=app;
		}
		
		/**
		 * 获取类 
		 * @param classname
		 * @return 
		 * 
		 */
		protected function GetClass(classname:String):Class
		{
			return (app.getDefinition(classname) as Class);
		}
		
		
		/**
		 * 设置资源  
		 * @param _resManager
		 * 
		 */
		public function setAppManager(_resManager:IApplicationManager):void{
			state|=RESOURCE;
			this.resManager=_resManager;
		}
		
		/**
		 * 获取ApplicationDomain; 
		 * @param url
		 * 
		 */
		public function GetApplication(url:String):ApplicationDomain{
			var result:ApplicationDomain=this.resManager.GetApplication(url);
			return result;
		}
		
		public function AddApplication(domain:String, app:ApplicationDomain):int
		{
			// TODO Auto Generated method stub
			return this.resManager.AddApplication(domain,app);;
		}
		
		public function AddLoadedList(domain:String, event:Event):int
		{
			// TODO Auto Generated method stub
			return this.resManager.AddLoadedList(domain,event);;
		}
		
		/**
		 * 
		 * @param domain
		 * @return 
		 * 
		 */
		public function GetLoaded(domain:String):Event
		{
			// TODO Auto Generated method stub
			return this.resManager.GetLoaded(domain);
		}
		
		/**
		 * 显示状态  
		 * @return 
		 */
		public function get ViewState():int{
			var result:int=this.state;
			return result;
		}
		/**
		 * 资源状态  
		 * @return 
		 * 
		 */
		public function get ResourceState():Boolean{
			return (state&RESOURCE)>0;
		}
		/**
		 * 获取事件绑定状态  
		 * @return 
		 * 
		 */
		public function get EventStatus():Boolean{
			return (state&EVENT)>0;
		}
		
		
		
		
		
		
		
		
		
		///////////////////////////////////////////////////////////////
		private static const ViewVector:Vector.<IView>=new Vector.<IView>;
		
		/**
		 * 注册模块 
		 * @param UI
		 * 
		 */
		public function registerView(UI:IView):int{
			var result:int=ViewVector.length;
			ViewVector.push(UI);
			return result;
		}
		
		/**
		 * 取回模块 
		 * @param ID
		 * @return 
		 * 
		 */
		public function retrieveView(ID:int):IView{
			var result:IView;
			try{
				result=ViewVector[ID];
				
			}catch(e:*){
				LogError(e,"retrieveMediator : 未找到ID");
			}
			return result;
		}
		
		////////////////////////////////////IWheel////////////////////////////////////
		
		private var _frameRate:uint;
		private var _timeWheel:Vector.<int>;
		
		public function get frameRate():uint{
			return _frameRate;
		}
		
		/**
		 * 开始循环帧频 
		 * 
		 */
		protected function startFrameWheel():void{
			FrameWheel.AddWheel(this);
		}
		/**
		 * 停止循环帧频 
		 * 
		 */
		protected function stopFrameWheel():void{
			FrameWheel.RemoveWheel(this);
		}
		/**
		 * 返回定时器区域 
		 * @return 
		 * 
		 */
		public function getWheelArea():Vector.<int>{
			return _timeWheel;
		}
		/**
		 * 设置定时器区域 
		 * @param timeWheel
		 * 
		 */
		public function setWheelArea(timeWheel:Vector.<int>):void{
			_timeWheel=timeWheel;
		}
		
		/**
		 * 帧频更新 
		 * 
		 */
		public function UpdateFrame():void{
			draw();
		}
		
		/**
		 * 重绘 
		 * 
		 */
		protected function draw():void{
			
		}
		
		/**
		 * 获取状态  
		 * @return 
		 * 
		 */
		public function get frameState():Boolean{
			return _frameState==FrameWheel.START;
		}
		/**
		 * 停止 
		 * 
		 */
		public function frameStop():void{ 
			_frameState=FrameWheel.STOP;
		}
		
		/**
		 * 定时器开始  
		 * 
		 */
		public function frameStart():void{
			_frameState=FrameWheel.START;
		}
	}
}